/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lt.cj.config.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lt.cj.config.EnterUrlsException;
import org.lt.cj.config.MissionConfigException;
import org.lt.cj.config.XMLConfigWriter;
import org.lt.cj.config.entities.TMallConfigModel;
import org.lt.cj.core.Seed;

/*
 * @author luohong
 */
public class MissionBuilder {

    private XMLConfigWriter wirter = new XMLConfigWriter();

    public TMallConfigModel buildTaoBaoMission() {

        TMallConfigModel config = new TMallConfigModel();
        config.setWebsiteName("taobao_mall");                                   //设置网站名
        config.setWebsiteUrl("http://www.tmall.com/?ver=2011b");                //设置网站地址
        //添加注释
        config.setComment("this is a document which record the mission config,for the god's sake");
        config.setTaskName("caiji_tmall");                                      //设置任务名
//        config.getUrls().add("http://www.tmall.com/?ver=2011b");        //设置网站入口地址
//        config.getUrls().add("http://item.tmall.com/item.htm?id=9671019071&prc=1&is_b=1");

        //设置网站入口种子列表
        List<Seed> list = new ArrayList();
        Seed seed = new Seed();
        seed.setSeedName("精品男装");
        seed.setUrl("http://list.tmall.com/50026329/g,guydamzxheytqorrhaydsojmguydamrwgmzdsorrgqztgnbmguydamzxheytsorxgi2tclbvgaydgnzzge3tunjwgm3cynjqgazdmmztgm5diobtgqwdkmbqgi3dgmzxhizdoobufq------------------------g-s-----42-0--50025174-x.htm?TBG=54527.101144.17&SPM=3.54527.101144.1.0.0.0&prt=1306042074427&prc=1");
        seed.setSortName("精品男装");
        list.add(seed);
        config.setSeeds(list);

        config.getFitUrlRegs().add("http://www\\.tmall\\.com/go/act/tmall/iwanttobuy\\.php.*");//匹配允许访问的URL的正则表达
        config.getFitUrlRegs().add("http://list\\.tmall\\.com/.*");             //匹配产品列表的页面URL的正则
        config.getFitUrlRegs().add("http://item\\.tmall\\.com/item\\.htm.*");  //匹配进入产品信息的页面URL的正则
        /*=======================*/
        config.setWorkingThreads(10); //设置线程数
        config.setPageEncoding("UTF-8");
        config.setOrigLanguage("zh-CN");//设置语言
        config.setTranLanguage("en");
        /*=======================*/
        //设置匹配抓取信息的产品页面Url
        List<String> pageUrlRegs = new ArrayList();
        pageUrlRegs.add("http://item\\.tmall\\.com/item\\.htm.*");
        config.setPageReg(pageUrlRegs);

        Map<String, List<String>> entityReg = new HashMap();
        //产品名的路径匹配
        List<String> productNameList = new ArrayList();
        productNameList.add("div class=\"layout grid-s5m0 \"");
        productNameList.add("div class=\"tb-detail-hd\"");
        productNameList.add("a target=\"_blank\" href=");
        entityReg.put("productName", productNameList);

        //产品价格的路径匹配
        List<String> productPriceList = new ArrayList();
        productPriceList.add("div class=\"tb-detail-bd tb-clear\"");
        productPriceList.add("ul class=\"tb-meta\"");
        productPriceList.add("li id=\"J_StrPriceModBox\" class=\"tb-detail-price tb-clearfix\"");
        entityReg.put("productPrice", productPriceList);

        //售出数量的路径匹配
        List<String> countSoldlist = new ArrayList();
        countSoldlist.add("div class=\"tb-detail-bd tb-clear\"");
        countSoldlist.add("ul class=\"tb-meta\"");
        countSoldlist.add("li class=\"tb-sold-out tb-clear\"");
        entityReg.put("countSold", countSoldlist);

        //商品信息路径匹配
        List<String> shopInfoCpList = new ArrayList();
        shopInfoCpList.add("div class=\"shop-intro\"");
        shopInfoCpList.add("div class=\"extend\"");
        shopInfoCpList.add("li");
        entityReg.put("shopInfo", shopInfoCpList);

        //宝贝信息的路径匹配
        List<String> productAttrList = new ArrayList();
        productAttrList.add("div id=\"attributes\" class=\"attributes");
        productAttrList.add("ul class=\"attributes-list");
        productAttrList.add("li");
        entityReg.put("productDetail", productAttrList);

        //产品图片的路径匹配
        List<String> photosList = new ArrayList();
        photosList.add("div class=\"tb-detail-bd tb-clear\"");
        photosList.add("div class=\"tb-gallery\"");
        photosList.add("div class=\"tb-booth tb-pic tb-s310\"");
        photosList.add("img id=\"J_ImgBooth\" src");
        entityReg.put("photosPath", photosList);

        //产品小图片的路径匹配
        List<String> thumbPhoList = new ArrayList();
        thumbPhoList.add("div class=\"tb-detail-bd tb-clear\"");
        thumbPhoList.add("div class=\"tb-gallery\"");
        thumbPhoList.add("ul id=\"J_UlThumb\" class=\"tb-thumb tb-clearfix\"");
        thumbPhoList.add("img src=");
        entityReg.put("thumbPhosPath", thumbPhoList);

        /* 产品详情的路径匹配 */
        //详情里的图片的路径匹配
        List<String> despPhosList = new ArrayList();
        despPhosList.add("script");
        entityReg.put("despPhos", despPhosList);

        //详情里的文本的路径匹配  to do已经不需要过滤了
//        List<String> despMemosList = new ArrayList();
//        despMemosList.add("div id=\"description\" class=\"ke-post J_DetailSection tshop-psm tshop-psm-bdetaildes\"");
//        despMemosList.add("div class=\"content\" id=\"J_DivItemDesc\"");
//        despMemosList.add("font");
//        entityReg.put("despMemos", despMemosList);
        /* end */

        // 店铺地址
        List<String> shopAddr = new ArrayList();
        shopAddr.add("div class=\"clearfix tb-header-nav\"");
        shopAddr.add("div class=\"nav\"");
        shopAddr.add("a href");
        entityReg.put("shopAddr", shopAddr);

        //评论列表的路径匹配
        List<String> commensList = new ArrayList();
        commensList.add("div class=\"tb-box tshop-psm tshop-psm-bdetailtabl\" id=\"J_Detail\"");
        commensList.add("div id=\"reviews\" class=\"J_DetailSection\" data-reviewApi");
        entityReg.put("commnents", commensList);

        //分类的路径匹配
        List<String> categoryList = new ArrayList();
        categoryList.add("ul class=\"mallCrumbs-nav\" id=\"J_crumbs\"");
        categoryList.add("li class=\"mallCrumbs-nav-item\"");
//        categoryList.add("a href");
        entityReg.put("category", categoryList);


        config.setEntityReg(entityReg);    //设置实体正则匹配

        return config;
    }

    public void createTaoBaoConfiFile(TMallConfigModel mission) {
        try {
            wirter.createConfigFile(wirter.buildUpMallDocument(mission), "c:/caiji_tmall.model");
        } catch (MissionConfigException ex) {
            Logger.getLogger(MissionBuilder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EnterUrlsException ex) {
            Logger.getLogger(MissionBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public XMLConfigWriter getWirter() {
        return wirter;
    }

    public void setWirter(XMLConfigWriter wirter) {
        this.wirter = wirter;
    }

    public static void main(String[] args) {

        MissionBuilder builder = new MissionBuilder();
        builder.createTaoBaoConfiFile(builder.buildTaoBaoMission());
    }
}
